package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.JdbcTemplate.HaiweidaCooperativeSchoolJdbc;
import com.wuseguang.springcloud.dao.jdbc.StudentCenterJdbc;
import com.wuseguang.springcloud.entity.Student;
import com.wuseguang.springcloud.service.StudentCenterService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.MyDES;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentCenterServiceImpl implements StudentCenterService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private HaiweidaCooperativeSchoolJdbc haiweidaCooperativeSchoolJdbc;

    @Autowired
    private StudentCenterJdbc centerJdbc;

    /**
     * @描述 登陆学生中心效验密码
     * @创建人 zm
     * @创建时间 2020/3/28
     */
    @Override
    public Result StudentCenterLogin(Integer id, String password) {//學生id
        //String sql = "SELECT  bancard_password  string FROM student WHERE id=?";
        String sql = "SELECT  phone  string FROM student WHERE id=?";
        String s = centerJdbc.queryString(sql, id);
        logger.info("StudentCenterLogin==>,s={},password={}", s, password);
        //if (StringUtils.isEmpty(s) || !MyDES.decryptBasedDes(s).equals(password)) {
        if (StringUtils.isEmpty(s) || !s.equals(password)) {
            logger.info("password error");
            return new Result(CodeUtil.PASSWORD_ERROR);
        }
        return new Result();
    }

    /**
     * @描述 学生中心
     * @创建人 zm
     * @创建时间 2020/3/28
     */
    @Override
    public Result queryStudentCenterAll(String deviceid) {
        Map<String, Object> stringObjectMap = centerJdbc.queryDevice(deviceid);
        if (stringObjectMap == null) {
            return new Result(CodeUtil.DEVICE_AFTER_OPERATION);
        }
        HashMap<String, Object> map = new HashMap<>();
        Integer classid = (Integer) stringObjectMap.get("classid");
        String class_icon = stringObjectMap.get("class_icon").toString();
        map.put("classid", classid);
        map.put("class_icon", class_icon);
        String sql = "SELECT id,student_name name,head_portrait FROM student WHERE class_id=?";
        List<Map<String, Object>> maps = centerJdbc.queryListMap(sql, classid);
        map.put("list", maps);
        return new Result(map);
    }

    /**
     * @描述 个人空间应用列表
     * @创建人 zm
     * @创建时间 2020/3/29
     */
    @Override
    public Result queryPersonalApplication(String deviceid, Integer id) {
        Map<String, Object> stringObjectMap = centerJdbc.queryDevice(deviceid);
        if (stringObjectMap == null) {
            return new Result(CodeUtil.DEVICE_AFTER_OPERATION);
        }
        Map<String, Object> map = new HashMap<>();
        String sql = "SELECT t.id tid,t.student_name tname,t.head_portrait,s.name cname,s.id cid FROM student t, classes s WHERE t.class_id=s.id AND t.id=?";
        Map<String, Object> map1 = centerJdbc.queryMap(sql, id);
        map.putAll(map1);
        sql = "SELECT j.id,j.image icon,j.name,j.url,j.type,j.sign FROM school_jurisdiction s,jurisdiction j WHERE s.jurisdiction_id=j.id AND s.bancard=1 AND s.school_id=? AND s.type=1  ORDER BY s.id";
        List<Map<String, Object>> maps = centerJdbc.queryListMap(sql, stringObjectMap.get("school_id"));
        map.put("list", maps);
        return new Result(map);
    }


    /**
     * @Author zm
     * @Description  根据学生id修改卡号
     * @Date 2020/10/15 11:32
     */
    @Override
    public Result updateStudentCardNumber(Integer id,String cardNumber) {
       String sql="UPDATE  student SET card_number=?  WHERE id=?";
        try {


        int i = centerJdbc.updateByParameter(sql, cardNumber, id);
        if (i>0){
            sql="select*from student where id=?";
            Student student = (Student)centerJdbc.query(sql, new BeanPropertyRowMapper<>(Student.class), id);
            logger.info("student={}",student);
            //海威达
            List<Student> hwdlist = new ArrayList<>();
            hwdlist.add(student);
            haiweidaCooperativeSchoolJdbc.haiweidaStudent(hwdlist);
        }
        }catch (Exception e) {
            logger.info("海威达同步学生失败 e", e);
        }
        return new Result();
    }

}
